package com.qfedu.bookstores.controller;

import com.qfedu.bookstores.entity.User;
import com.qfedu.bookstores.service.IUserService;
import com.qfedu.bookstores.service.impl.IUserServiceImpl;
import com.qfedu.bookstores.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

//用户模块即要处理登录也要处理注册
@WebServlet(name = "UserServlet",value="/userServlet")
public class UserServlet extends BaseServlet {
    private IUserService userService = new IUserServiceImpl();

    /**
     * 处理登录逻辑操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取参数
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
        User user = WebUtils.copyParamToBean(request.getParameterMap(), new User());
        //2.调用业务逻辑登录
        User login = userService.login(user);
        //3.判断返回的对象值
        if(Objects.isNull(login)){
            //登录失败
            //提示回显示信息和保存用户数据
            request.setAttribute("msg", "用户名或密码错误");
            request.setAttribute("username", user.getUsername());
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else{
            //登录成功
            //将登录成功传递用户信息保存在session作用域
            request.getSession().setAttribute("user", login);
            request.getRequestDispatcher("pages/user/login_success.jsp").forward(request, response);
        }
    }
    /**
     * 处理登录逻辑操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.接收参数
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String email = request.getParameter("email");
        //getParameterMap --> 可以一次性读取传递参数值存在map集合中
        User user = WebUtils.copyParamToBean(request.getParameterMap(), new User());
        String code = request.getParameter("code");
        //2.读取Session对象中验证码并和前端获取的验证码验证
        String vcode = (String)request.getSession().getAttribute("code");
        if(vcode.equalsIgnoreCase(code)){
            //3.触发业务成逻辑调用验证是否存在注册用户名
            if(userService.existsUserName(user.getUsername())){
                request.setAttribute("msg", "用户名已经存在无法注册!");
                request.setAttribute("email",user.getEmail());
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            }else{
                //注册
                userService.registerUser(user);
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }

        }else{//验证码错误
            request.setAttribute("msg", "验证码错误!");
            request.setAttribute("username",user.getUsername());
            request.setAttribute("email", user.getEmail());
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);

        }
    }

    /**
     * 注销方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          //立即销毁session
          request.getSession().invalidate();
          //重定向到主页
         response.sendRedirect(request.getContextPath());
    }
}
